<?php
class OrderController extends Zend_Controller_Action{
	public $_model = null;
	public function init(){
		$this->_model = new Application_Model_DbTable_Order();	
			
		//thiet lap layout
		$this->view->layout()->placeholders = array(
				'slider'=>array('status'=>false,'content'=>''),
				'search'=>array('status'=>false,'content'=>'')
		);
		$card = new Zend_Session_Namespace("card");
		
		//thiết lập giá trị ban đầu
		if(!isset($card->total)){
			$card->total=0;
			$card->items=array();
		}
		//$this->view->headScript()->appendFile(PUBLIC_RESOURCE.'/skins/default/js/default.js');
	}
	
	public function indexAction(){
		//var_dump( $_SESSION);
	}
	
	public function insertAction(){
		$id = $this->_request->getParam('id');
		$qty= $this->_request->getParam('qty');
		$card = new Zend_Session_Namespace("card");
		
		//nếu id chưa tồn tại
		if(!isset($card->items[$id])){
			$card->items[$id] = $qty;
			$card->total += $qty;
		}else{
			$card->items[$id] += $qty;
			$card->total += $qty;
		}
		
		//chuyen ve action index
		//$this->_helper->redirector('index');
		$this->redirect($this->view->baseUrl('/'.LANG.'/gio-hang.html'));
		
	}
	public function updateAction(){
		$card = new Zend_Session_Namespace('card');
		$countItems = 1;
		$total=0;
		foreach($card->items as $key => $value){
			$qty = 'qty'.$countItems;
			$val = $this->_request->getParam($qty);
			
			//nếu rỗng
			if($val=="" || $val<=0)
			{
				$val=0;
			}
			//nếu không là số
			if(is_numeric($val)==false){
				$val=0;
			}
			
			$card->items[$key]=$val;
			$total+=$val;
			if($val==0){
				unset($card->items[$key]);
			}
			$countItems++;
		}
		$card->total=$total;
		//$this->_helper->redirector('index');
		$this->redirect($this->view->baseUrl('/'.LANG.'/gio-hang.html'));
	}
	public function deleteAction(){
		$id=$this->_request->getParam('id');
		$productMD = new Application_Model_DbTable_Product();
		$product=$productMD->fetchRow($productMD->select()->where('product_id = ?',$id));
		if($product){
			$card = new Zend_Session_Namespace("card");
			$card->total=$card->total - $card->items[$id];
			unset($card->items[$id]);
			//$this->_helper->json->sendJson($card->total);
		}
		//$this->redirect($this->view->baseUrl('/'.LANG.'/gio-hang.html'));
		$this->_helper->layout->disableLayout();
		$this->_helper->json->sendJson($card->total);
	}
	
	public function tempAction(){
		$this->_helper->layout()->disableLayout(true);
		$card = new Zend_Session_Namespace("card");
		$this->view->card  =$card;
		$this->view->headTitle()->prepend('Giỏ hàng');
	
	}
	public function destroyAction(){
		  Zend_Session::namespaceUnset("card");
          $card = new Zend_Session_Namespace("card");
          $this->redirect($this->view->baseUrl('/'.LANG.'/gio-hang.html'));
        //$this->_helper->redirector('index');
	}
	
	public function orderAction(){
		$this->view->headTitle()->prepend('Đặt hàng');
		//trạng thái chưa đặt hàng
		$pagestatus = false;
		$auth = Zend_Auth::getInstance();
		$card = new Zend_Session_Namespace("card");

			if($auth->hasIdentity()){
				
				//lấy thông tin giỏ hàng
				//lấy thông tin người dùng
				$form = new Default_Form_Order();
				$orderID = $auth->getIdentity()->user_id;
				$userMD = new Application_Model_DbTable_User();
				$row = $userMD->fetchRow("user_id = $orderID");
				$form->populate($row->toArray()); 
				$this->view->form = $form;
				
				//đặt hàng
				if($this->_request->isPost()){
					if($form->isValid($_POST)){
						
						//đơn hàng
						$orderRow = $this->_model->createRow();
						//$orderRow->order_code = date("YmdHis").rand(1000, 9999);
						$orderRow->order_code = time().$auth->getIdentity()->user_id;
						$orderRow->order_status = 0;
						$orderRow->order_user_id = $orderID;
						$orderRow->order_note = $form->getValue('user_note');
						$orderRow->order_creation_time = date('Y-m-d H:i:s');
						$orderRow->order_address = $form->getValue('user_address');
						$orderRow->order_mobile = $form->getValue('user_phone');
						$orderRow->save();
						
						//chi tiết đơn hàng
						$orderDetailModel = new Application_Model_DbTable_OrderDetail();
						foreach($card->items as $id => $qty){
							$orderDetailRow = $orderDetailModel->createRow();
							$orderDetailRow->order_detail_order_code = $orderRow->order_code;
							$orderDetailRow->order_detail_product_id = $id;
							$orderDetailRow->order_detail_product_quantity = $qty;
							$orderDetailRow->save();
						}
						
						//xóa bỏ giỏ hàng hiện tại
						Zend_Session::namespaceUnset("card");
						$card = new Zend_Session_Namespace("card");
						$this->_helper->flashMessenger('Đặt hàng thành công!');
						$this->redirect($this->view->baseUrl('/'.LANG.'/thong-bao.html'));
						
					}//end check valid
				}//end post			
			}//end login
			$this->view->card  =$card;
	}// end action
}